
package com.hgk.common.common.ui.adapter;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.common.dongqiuhui.apps.comm.tools.BallUtil;
import com.common.nostra13.universalimageloader.core.DisplayImageOptions;
import com.common.nostra13.universalimageloader.core.ImageLoader;
import com.common.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.common.nostra13.universalimageloader.core.download.ImageDownloader.Scheme;
import com.hgk.common.common.ui.activity.PicSelectActivity;
import com.hgk.common.common.ui.model.PhotosBean;
import com.hgk.common.common.util.LoadImageDisplayOption;

import java.util.List;

import width.hgk.wanwan.R;

public class PicPhotoSelectAdapter extends BaseAdapter {

	private List<PhotosBean> mSList;
	private Context mContext;
	private int mLimitCount=0;
	private LayoutInflater mLayoutInflater;
	private ImageLoader mImageLoader;
	private DisplayImageOptions mDisplayImageOptions;
	private int mScreenWidth=0,mImageSize=0;
	private ImageView mIvSelected;
	public PicPhotoSelectAdapter(Context context, List<PhotosBean> slist, int limitCount) {
		mContext=context;
		mSList=slist;
		mLimitCount=limitCount;
		mLayoutInflater= LayoutInflater.from(mContext);
		mImageLoader=ImageLoader.getInstance();
		mImageLoader.init(ImageLoaderConfiguration.createDefault(mContext));
		mDisplayImageOptions= LoadImageDisplayOption.getDefaultIcon(R.drawable.default_pic);
		mScreenWidth=BallUtil.getScreenWidth((Activity)mContext);
		mImageSize=(mScreenWidth-4)/3;
	}
	@Override
	public int getCount() {
		return mSList == null ? 0 : mSList.size();
	}

	@Override
	public Object getItem(int position) {
		return (mSList == null || mSList.size() <= position) ? null : mSList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder=null;
		if(convertView==null){
			viewHolder=new ViewHolder();
			convertView=mLayoutInflater.inflate(R.layout.pic_photo_select_item, null);
			viewHolder.mIvPic=(ImageView)convertView.findViewById(R.id.iv_pic_select_item);
			viewHolder.mIvSelect=(ImageView)convertView.findViewById(R.id.iv_pic_select);
			RelativeLayout.LayoutParams lp=(RelativeLayout.LayoutParams)viewHolder.mIvPic.getLayoutParams();
			lp.width=mImageSize;
			lp.height=mImageSize;
			convertView.setTag(viewHolder);
		}else{
			viewHolder=(ViewHolder)convertView.getTag();
		}
		PhotosBean bean=(PhotosBean) getItem(position);
		if(bean!=null){
			if(position==0){
				viewHolder.mIvPic.setImageResource(R.drawable.icon_add_photo_smal);

				mImageLoader.displayImage(Scheme.DRAWABLE.wrap(String.valueOf(R.drawable.icon_add_photo_smal)), viewHolder.mIvPic, mDisplayImageOptions);
				viewHolder.mIvSelect.setVisibility(View.GONE);
				viewHolder.mIvPic.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						((PicSelectActivity)mContext).seePoto(position);
					}
				});
				return convertView;
			}
			if(!TextUtils.isEmpty(bean.getPic_path())){
				mImageLoader.displayImage(Scheme.FILE.wrap(bean.getPic_path()), viewHolder.mIvPic,mDisplayImageOptions);
			}
			viewHolder.mIvSelect.setVisibility(View.VISIBLE);
			if(bean.getIs_select()==1){
				viewHolder.mIvSelect.setImageResource(R.drawable.duihao_3x);
			}else{
				viewHolder.mIvSelect.setImageResource(R.drawable.duihaohui_3x);
			}
			viewHolder.mIvSelect.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					mIvSelected=(ImageView) v;
					((PicSelectActivity)mContext).addPic(position);
				}
			});
			viewHolder.mIvPic.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					((PicSelectActivity)mContext).seePoto(position);
					
				}
			});
		}
		return convertView;
	}
	public void setImageSelected(int select){
		if(select==1){
			mIvSelected.setImageResource(R.drawable.duihao_3x);
		}else if(select==0){
			mIvSelected.setImageResource(R.drawable.duihaohui_3x);
		}
	}
	class ViewHolder{
		public ImageView mIvPic;
		public ImageView mIvSelect;
	}
}
